<?php

namespace common\models;

use Yii;
use common\util\CommonUtil;
use common\widgets\ajaxbtn\AjaxUtil;
use common\widgets\ajaxbtn\Status;

/**
 * This is the model class for table "tag".
 *
 * @property integer $id
 * @property string $name
 * @property string $pinyin
 */
class Tag extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'tag';
    }
    
    
    //新增 or 保存   事务
    public function saveWithTrans(){
        
        
        $transaction = Yii::$app->db->beginTransaction();
    
        $errors = [];
        try {
    
            //1.更新级联的NEWS数据
            if(($this->oldAttributes['pinyin'] != $this->pinyin || $this->oldAttributes['name'] != $this->name) && !$this->isNewRecord){    //修改时，当pinyin有改变时则将级联的news数据进行更新
                
                
                $oldjson = '{"name":"'.$this->oldAttributes['name'].'","pinyin":"'.$this->oldAttributes['pinyin'].'"}';
                $newjson = '{"name":"'.$this->name.'","pinyin":"'.$this->pinyin.'"}';
                
                $sql = "UPDATE news SET tag= REPLACE(tag,'{$oldjson}','{$newjson}')";
                $result = News::getDb()->createCommand($sql)->execute();
                if($result === false){
                    $transaction->rollBack();
                    AjaxUtil::returnBack(Status::$ERROR,'服务器繁忙，请稍后重试！');
                }
                
                
            }
            
            //2.新增或修改 TAG数据
            if(!$this->save()){
                $errors = $this->errors;
                throw new \Exception();
            }
            
            $transaction->commit();
        } catch (\Exception $e) {
            $transaction->rollBack();
        }
        return $errors;
    }
    
    //删除   事务
    public function deleteWithTrans(){
    
    
        $transaction = Yii::$app->db->beginTransaction();
    
        $errors = [];
        try {
    
            //1.更新级联的NEWS数据
            
            $oldjson = '{"name":"'.$this->oldAttributes['name'].'","pinyin":"'.$this->oldAttributes['pinyin'].'"}';
            
            $sql1 = "UPDATE news SET tag= REPLACE(tag,',{$oldjson}','')";
            $sql2 = "UPDATE news SET tag= REPLACE(tag,'{$oldjson},','')";
            $sql3 = "UPDATE news SET tag= REPLACE(tag,'{$oldjson}','')";
            $result1 = News::getDb()->createCommand($sql1)->execute();
            $result2 = News::getDb()->createCommand($sql2)->execute();
            $result3 = News::getDb()->createCommand($sql3)->execute();
            if($result1 === false || $result2 === false || $result3 === false){
                $transaction->rollBack();
                return $this->redirect(['index']);
            }
    
            //2.删除 TAG数据
            if(!$this->delete()){
                $errors = $this->errors;
                throw new \Exception();
            }
    
            $transaction->commit();
        } catch (\Exception $e) {
            $transaction->rollBack();
        }
        return $errors;
    }
    
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name'], 'required'],
            [['name'], 'unique', 'message' => '该标签已经存在！'],
            [['id'], 'integer'],
            [['name'], 'string', 'max' => 120],
            [['pinyin'], 'string', 'max' => 200],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => '标签名称',
            'pinyin' => '字母拼音',
        ];
    }
}
